<?php
/**
 * @name 发货单Service
 * @Author : yu
 * @Created : 2016-11-1
 * @Copyright : MPE.Fiidee.Monda.Cn
 */
namespace trading\service;

use herosphp\core\Loader;

use herosphp\utils\Page;
use utils\x1\MyFunction;
use utils\x1\MyNumber;

Loader::import('utils.x1.MyFunction');

class InvoiceService extends CommonService{

    private $thisModel=null;

    public function __construct()
    {
        $this->thisModel=Loader::model('tradingInvoice');
    }
    //发货单搜索
    public function search($pagesize, $page, $search_arr)
    {

        $offset = $pagesize * ($page - 1); //计算记录偏移量

        $whereSql = "And invoice.app_id='".appId()."'";

        //导出Excel add By Laoliu
        if( $search_arr['ids'] ){
            $whereSql .= " AND invoice.id in {$search_arr['ids']}";
        }

        if ($search_arr['start_time'] != '' || $search_arr['end_time'] != '') {

            $whereSql .= " And " . MyFunction::betweenSqlByTime('unix_timestamp(contract.signed_time)', strtotime($search_arr['start_time']), strtotime($search_arr['end_time']));

        }

        if (in_array($search_arr['select_mix_option1'], array('contract_no', 'order_no',  'invoice_no'))) {
            switch ($search_arr['select_mix_option1']) {
                case 'contract_no':

                    $whereSql .= " And contract.{$search_arr['select_mix_option1']} Like '%{$search_arr['select_mix_option_value1']}%'";
                    break;
                case 'order_no':

                    $whereSql .= " And o.{$search_arr['select_mix_option1']} Like '%{$search_arr['select_mix_option_value1']}%'";
                    break;

                case 'invoice_no':
                    $whereSql .= " And invoice.{$search_arr['select_mix_option1']} Like '%{$search_arr['select_mix_option_value1']}%'";
                    break;
            }
        }

        if (($search_arr['start_time'] != '' || $search_arr['end_time'] != '') && in_array($search_arr['select_mix_option2'], array('order_time', 'pay_time', 'delivery_time'))) {
            switch ($search_arr['select_mix_option2']) {
                case 'order_time':
                    $whereSql .= " And " . MyFunction::betweenSqlByTime('unix_timestamp(o.order_time)', strtotime($search_arr['start_time']), strtotime($search_arr['end_time']));
                    break;

                case 'pay_time':
                    $whereSql .= " And " . MyFunction::betweenSqlByTime('unix_timestamp(o.pay_time)', strtotime($search_arr['start_time']), strtotime($search_arr['end_time']));
                    break;

                case 'delivery_time':
                    $whereSql .= " And " . MyFunction::betweenSqlByTime('unix_timestamp(invoice.delivery_time)', strtotime($search_arr['start_time']), strtotime($search_arr['end_time']));
                    break;

            }
        }

        $count_sql = "Select  count(*)
                From trading_invoice  As invoice
                Left Join trading_contract as contract On contract.order_id=invoice.order_id
                Left Join trading_order As o On invoice.order_id=o.id
                Left Join member As m On m.id=o.member_id
                Where 1 $whereSql";
        $total = $this->thisModel->getDB()->excute($count_sql)->fetchColumn();

        //分页查询
        $pageinationSql = isset ($offset) ? " Limit $offset,$pagesize" : " Limit 0,$pagesize";

        $sql = "Select contract.*,invoice.*,invoice.id as invoice_id,o.*,m.username
                 From trading_invoice  As invoice
                Left Join trading_contract as contract On contract.order_id=invoice.order_id
                Left Join trading_order As o On invoice.order_id=o.id
                Left Join member As m On m.id=o.member_id
                Where 1 $whereSql
                Order By invoice.delivery_time Desc $pageinationSql";

        $items = $this->thisModel->getDB()->excute($sql)->fetchAll($fetch_style = 2);

        $pageHandler = new Page($total, $pagesize, $page);

        $pageData = $pageHandler->getPageData(DEFAULT_PAGE_STYLE);

        return array("items" => $items, "pageData" => $pageData, "total" => $total);
    }

    //获取发货单
    public function getInvoiceByOrderId($order_id){

        return $this->thisModel->getItem(array('order_id' => $order_id));

    }

    /**
     * 订单生成发货单
     * @param $member_id 订单用户id
     * @param $order_id 订单id
     * @param $delivery_time 发货时间
     * @return int
     */
    public function addInvoice($order_id,$member_id,$delivery_time)
    {

        if ($this->thisModel->getItem(array('order_id' => $order_id))) {

            return false;

        } else {


            $myNumber = new MyNumber(appId());
            $invoice_no = $myNumber->build_no('fh');//生成销售单号

            $addData = array(
                "invoice_no" => $invoice_no,
                "order_id" => $order_id,
                "delivery_time" => $delivery_time,
                "member_id" => $member_id,
            );
            return $this->thisModel->add($addData);
        }
    }

    /**
     * 更新发货单，收货时间
     * @param $order_id
     * @param $confirm_delivery_time
     */
    public function updateInvoice($order_id,$member_id,$confirm_delivery_time){

      $bool=$this->thisModel->updates(array('confirm_delivery_time'=>$confirm_delivery_time),array('order_id'=>$order_id,'member_id'=>$member_id));
      return $bool;
    }


}

